package com.xbongbong.paas.util;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.exception.WorkflowException;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;

import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/24 10:00
 */
public class WorkflowExplainUtil {

    /**
     * 获取字段
     * @param workOrderSubExplainMap
     * @param subExplainMap
     * @param formId
     * @param businessType
     * @param valueType
     * @param attr
     * @return
     */
    public static FieldAttrEntity getThisSubField(Map<Long, Map<String, Map<String, FieldAttrEntity>>> workOrderSubExplainMap, Map<Long, Map<String,
            Map<String, FieldAttrEntity>>> subExplainMap, Long formId, Integer businessType,
                                            Integer valueType, String attr, String subAttr) throws XbbException {
        FieldAttrEntity fieldAttrEntity = null;
        if (Objects.equals(WorkflowValueTypeEnum.DYNAMIC_VALUE.getType(), valueType)) {
            Map<Long, Map<String, Map<String, FieldAttrEntity>>> explainMap;
            if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), businessType)) {
                explainMap = workOrderSubExplainMap;
            } else {
                explainMap = subExplainMap;
            }
            if (!explainMap.containsKey(formId)) {
                throw new WorkflowException(I18nStringConstant.FORM_NOT_EXIST);
            }
            if (!explainMap.get(formId).containsKey(attr)) {
                throw new WorkflowException(BusinessRuleErrorCodeEnum.API_ERROR_261006.getMsg());
            }
            if (!explainMap.get(formId).get(attr).containsKey(subAttr)) {
                throw new WorkflowException(BusinessRuleErrorCodeEnum.API_ERROR_261006.getMsg());
            }
            fieldAttrEntity = explainMap.get(formId).get(attr).get(subAttr);
        }
        return fieldAttrEntity;
    }

    /**
     * 获取目标表单的字段
     * @param workOrderSubExplainMap
     * @param subExplainMap
     * @param workflowNodeEntity
     * @param key
     * @return
     */
    public static FieldAttrEntity getTargetSubField(Map<Long, Map<String, Map<String, FieldAttrEntity>>>  workOrderSubExplainMap, Map<Long, Map<String, Map<String, FieldAttrEntity>>>  subExplainMap,
                                              WorkflowNodeEntity workflowNodeEntity, String key, String subKey) throws XbbException {
        FieldAttrEntity targetSubField;
        Map<Long, Map<String, Map<String, FieldAttrEntity>>> explainMap;
        if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), workflowNodeEntity.getBusinessType())) {
            explainMap = workOrderSubExplainMap;
        } else {
            explainMap = subExplainMap;
        }
        Long formId = workflowNodeEntity.getFormId();
        if (!explainMap.containsKey(workflowNodeEntity.getFormId())) {
            throw new WorkflowException(I18nStringConstant.FORM_NOT_EXIST);
        }
        if (!explainMap.get(formId).containsKey(key)) {
            throw new WorkflowException(BusinessRuleErrorCodeEnum.API_ERROR_261006.getMsg());
        }
        if (!explainMap.get(formId).get(key).containsKey(subKey)) {
            throw new WorkflowException(BusinessRuleErrorCodeEnum.API_ERROR_261006.getMsg());
        }
        targetSubField = explainMap.get(workflowNodeEntity.getFormId()).get(key).get(subKey);
        return targetSubField;
    }

    /**
     * 获取字段
     * @param explainMap
     * @param formId
     * @param businessType
     * @param valueType
     * @param attr
     * @return
     */
    public static FieldAttrEntity getThisField(Map<String, Map<String, FieldAttrEntity>> explainMap, Long formId, Integer businessType,
                                         Integer valueType, String attr) throws XbbException {
        FieldAttrEntity fieldAttrEntity = null;
        if (Objects.equals(WorkflowValueTypeEnum.DYNAMIC_VALUE.getType(), valueType)) {
            String formIdStr;
            if (Objects.equals(XbbRefTypeEnum.WORK_ORDER.getCode(), businessType)) {
                formIdStr = PaasConstant.WORK_ORDER + formId;
            } else {
                formIdStr = formId.toString();
            }
            if (!explainMap.containsKey(formIdStr)) {
                throw new WorkflowException(I18nStringConstant.FORM_NOT_EXIST);
            }
            if (!explainMap.get(formIdStr).containsKey(attr)) {
                throw new WorkflowException(BusinessRuleErrorCodeEnum.API_ERROR_261006.getMsg());
            }
            fieldAttrEntity = explainMap.get(formIdStr).get(attr);
        }
        return fieldAttrEntity;
    }
}
